<template>
  <div>
    <c-table-box ref="cTableBox" hide-func checkbox :other-filters="otherFilters" :table-config="tableConfig" @selection-change="selectionChange">
      <template #btn>
        <c-permission v-if="type==='all'" code="all">
          <ActiveCheckTask :sel-data="selections" opt-code="activate" @on-success="refreshTableData" />
          <c-button type="primary" opt-code="changeAcceptPerson" @click="handleChangeAcceptPerson">变更验收人员</c-button>

        </c-permission>
        <c-permission v-else-if="type==='waitApply'" code="application">
          <c-button type="primary" opt-code="application" @click="clickToRouterHandle('CheckSoftApply')">报验</c-button>
        </c-permission>
        <c-permission v-else-if="type==='audit'" code="audit">
          <Audit :sel-data="selections" opt-code="audit" type="SOFT" @on-success="refreshTableData" />
        </c-permission>
        <c-permission v-else-if="type==='planCheck'" code="arrangeapplication">
          <PlanCheck :sel-data="selections" opt-code="arrangeapplication" @on-success="refreshTableData" />
          <Exception :sel-data="selections" opt-code="exception" @on-success="refreshTableData" />
        </c-permission>
        <c-permission v-else-if="type==='waitCheck'" code="check">
          <c-button type="primary" opt-code="execute" @click="clickToRouterHandle('CheckSoftExecute')">验收执行</c-button>
        </c-permission>
        <c-permission v-else-if="type==='verify'" code="verify">
          <c-button type="primary" opt-code="verify" @click="clickToRouterHandle('CheckSoftVerify')">核对结案类型</c-button>
        </c-permission>
      </template>
    </c-table-box>
    <person-dlg
      v-if="showAcceptorDlg"
      ref="designerDlg"
      :visible.sync="showAcceptorDlg"
      :detail-data="activedData"
      @success="refreshTableData"
    />
  </div>
</template>
<script>
import Exception from '../../components/exception'
import { otherFiltersItem } from '@/utils'
import ActiveCheckTask from '../../components/activeCheckTask'
import Audit from '../../components/audit'
import PlanCheck from '../../components/planCheck'
import { queryDecorateTask } from '@/api/check'
import personDlg from '../../components/acceptPersonDlg'

export default {
  name: 'List',
  components: {
    ActiveCheckTask,
    Audit,
    PlanCheck,
    Exception,
    personDlg
  },
  props: {
    type: {
      type: String,
      default: ''
    },
    serachFilters: {
      type: Array,
      default: () => []
    },
    activeType: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      tableConfig: {
        interfacePath: '/slm-admin/api/v1/shopAccept/selectPage',
        hideFuncBar: true,
        sort: 'createTime',
        order: 'desc',
        apiHeader: {
          'Permission-perCode': 'checksoft',
          'Permission-App': 'slm'
        },
        setting: []
      },
      allSetting: [
        {
          prop: 'constCode',
          label: '门店建设项目号',
          width: 180
        },
        {
          prop: 'acceptTaskCode',
          label: '验收任务号',
          width: 200,
          className: 'btn-green',
          isButton: true,
          clickFun: this.toDetail
        },
        {
          prop: 'accApplyPassTime',
          label: '验收申请审核通过时间',
          width: 180
        },
        {
          prop: 'acceptType',
          label: '验收类型',
          width: 120,
          propEnum: {
            appCode: 'slm',
            domainCode: 'marketing',
            dictTypeCode: 'ACCEPT_TYPE'
          }
        },
        {
          prop: 'haveFrock',
          label: '是否含公装',
          width: 120,
          propEnum: {
            appCode: 'slm',
            domainCode: 'marketing',
            dictTypeCode: 'BOOLEANS'
          }
        },
        {
          prop: 'acceptSchedule',
          label: '验收进度',
          width: 120,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'DECORATION_SCHEDULE'
          }
        },
        {
          prop: 'acceptSteps',
          label: '验收步骤',
          width: 120,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'SHOP_DECORATION_STEPS'
          }
        },
        // {
        //   prop: 'expectAcceptApplyReportTim',
        //   label: '预计验收申请提报时间'
        // },
        // {
        //   prop: 'isOutTimeUnaccept',
        //   label: '是否超期未报验',
        //   propEnum: {
        //     appCode: 'slm',
        //     domainCode: 'marketing',
        //     dictTypeCode: 'BOOLEANS'
        //   }
        // },
        {
          prop: 'acceptEndType',
          label: '结案类型',
          width: 120,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'DECORATION_CLOSED_TYPE'
          }
        },
        {
          prop: 'shopName',
          label: '所属门店',
          width: 120
        },
        {
          prop: 'clientName',
          label: '所属客户',
          width: 120
        },
        {
          prop: 'officeName',
          label: '所属办事处',
          width: 120
        },
        {
          prop: 'range',
          label: '装修范围',
          width: 120,
          render(h, row) {
            return h('c-dict-range-view', {
              props: {
                dictTypeCode: 'DECORATION_RANGE',
                value: row.range
              }
            })
          }
        },
        {
          prop: 'constUnitName',
          label: '施工单位',
          width: 120
        },
        {
          prop: 'principal',
          label: '施工负责人',
          width: 120
        },
        {
          prop: 'proSupervisorName',
          label: '工程监理',
          width: 120
        },
        {
          prop: 'formSupervisorName',
          label: '形象督导',
          width: 120
        },
        {
          prop: 'marketType',
          label: '报图类型',
          width: 120,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'MARKET_TYPE'
          }
        },
        {
          prop: 'constType',
          label: '门店建设类型',
          width: 140,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'CONST_TYPE'
          }
        },
        {
          prop: 'exceptionName',
          width: 100,
          label: '异常类型'
        },
        {
          prop: 'exceptionDesc',
          minWidth: 120,
          label: '异常说明'
        }
      ],
      waitApplySetting: [
        {
          prop: 'constCode',
          label: '门店建设项目号',
          width: 180
        },
        {
          prop: 'acceptTaskCode',
          label: '验收任务号',
          width: 200,
          className: 'btn-green',
          isButton: true,
          clickFun: this.toDetail
        },
        {
          prop: 'accApplyPassTime',
          label: '验收申请审核通过时间',
          width: 180
        },
        {
          prop: 'acceptType',
          label: '验收类型',
          width: 120,
          propEnum: {
            appCode: 'slm',
            domainCode: 'marketing',
            dictTypeCode: 'ACCEPT_TYPE'
          }
        },
        {
          prop: 'haveFrock',
          label: '是否含公装',
          width: 120,
          propEnum: {
            appCode: 'slm',
            domainCode: 'marketing',
            dictTypeCode: 'BOOLEANS'
          }
        },
        {
          prop: 'acceptSchedule',
          label: '验收进度',
          width: 120,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'DECORATION_SCHEDULE'
          }
        },
        {
          prop: 'acceptSteps',
          label: '验收步骤',
          width: 120,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'SHOP_DECORATION_STEPS'
          }
        },
        // {
        //   prop: 'expectAcceptApplyReportTim',
        //   label: '预计验收申请提报时间'
        // },
        // {
        //   prop: 'isOutTimeUnaccept',
        //   label: '是否超期未报验',
        //   propEnum: {
        //     appCode: 'slm',
        //     domainCode: 'marketing',
        //     dictTypeCode: 'BOOLEANS'
        //   }
        // },
        {
          prop: 'shopName',
          label: '所属门店',
          width: 120
        },
        {
          prop: 'clientName',
          label: '所属客户',
          width: 120
        },
        {
          prop: 'officeName',
          label: '所属办事处',
          width: 120
        },
        {
          prop: 'range',
          label: '装修范围',
          width: 120,
          render(h, row) {
            return h('c-dict-range-view', {
              props: {
                dictTypeCode: 'DECORATION_RANGE',
                value: row.range
              }
            })
          }
        },
        {
          prop: 'constUnitName',
          label: '施工单位',
          width: 120
        },
        {
          prop: 'principal',
          label: '施工负责人',
          width: 120
        },
        {
          prop: 'proSupervisorName',
          label: '工程监理',
          width: 120
        },
        {
          prop: 'formSupervisorName',
          label: '形象督导',
          width: 120
        },
        {
          prop: 'marketType',
          label: '报图类型',
          width: 120,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'MARKET_TYPE'
          }
        },
        {
          prop: 'constType',
          label: '门店建设类型',
          width: 140,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'CONST_TYPE'
          }
        },
        {
          prop: 'exceptionName',
          width: 100,
          label: '异常类型'
        },
        {
          prop: 'exceptionDesc',
          minWidth: 120,
          label: '异常说明'
        }
      ],
      otherSetting: [
        {
          prop: 'constCode',
          label: '门店建设项目号',
          width: 180
        },
        {
          prop: 'acceptTaskCode',
          label: '验收任务号',
          width: 200,
          className: 'btn-green',
          isButton: true,
          clickFun: this.toDetail
        },
        {
          prop: 'accApplyPassTime',
          label: '验收申请审核通过时间',
          width: 180
        },
        {
          prop: 'acceptType',
          label: '验收类型',
          width: 120,
          propEnum: {
            appCode: 'slm',
            domainCode: 'marketing',
            dictTypeCode: 'ACCEPT_TYPE'
          }
        },
        {
          prop: 'haveFrock',
          label: '是否含公装',
          width: 120,
          propEnum: {
            appCode: 'slm',
            domainCode: 'marketing',
            dictTypeCode: 'BOOLEANS'
          }
        },
        {
          prop: 'acceptSchedule',
          label: '验收进度',
          width: 120,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'DECORATION_SCHEDULE'
          }
        },
        {
          prop: 'acceptSteps',
          label: '验收步骤',
          width: 120,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'SHOP_DECORATION_STEPS'
          }
        },
        {
          prop: 'shopName',
          label: '所属门店',
          width: 120
        },
        {
          prop: 'clientName',
          label: '所属客户',
          width: 120
        },
        {
          prop: 'officeName',
          label: '所属办事处',
          width: 120
        },
        {
          prop: 'range',
          label: '装修范围',
          width: 120,
          render(h, row) {
            return h('c-dict-range-view', {
              props: {
                dictTypeCode: 'DECORATION_RANGE',
                value: row.range
              }
            })
          }
        },
        {
          prop: 'constUnitName',
          label: '施工单位',
          width: 120
        },
        {
          prop: 'principal',
          label: '施工负责人',
          width: 120
        },
        {
          prop: 'proSupervisorName',
          label: '工程监理',
          width: 120
        },
        {
          prop: 'formSupervisorName',
          label: '形象督导',
          width: 120
        },
        {
          prop: 'marketType',
          label: '报图类型',
          width: 120,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'MARKET_TYPE'
          }
        },
        {
          prop: 'constType',
          label: '门店建设类型',
          width: 140,
          propEnum: {
            domainCode: 'marketing',
            appCode: 'slm',
            dictTypeCode: 'CONST_TYPE'
          }
        },
        {
          prop: 'exceptionName',
          width: 100,
          label: '异常类型'
        },
        {
          prop: 'exceptionDesc',
          minWidth: 120,
          label: '异常说明'
        }
      ],
      typeFilters: [],
      activedData: {},
      showAcceptorDlg: false,
      selections: []
    }
  },
  computed: {
    otherFilters() {
      return [...this.serachFilters, ...this.typeFilters]
    }
  },
  watch: {
    type: {
      handler(v) {
        this.setOtherFilters(v)
        this.setTableSetting(v)
      },
      immediate: true
    },
    serachFilters: {
      handler(v) {
        if (this.activeType !== this.type) return
        this.refreshTableData()
      }
    }
  },
  created() {
  },
  activated() {
    if (this.ready && this.refreshTableData) {
      this.refreshTableData()
    }
  },
  deactivated() {
    this.ready = true
  },
  methods: {
    handleChangeAcceptPerson() {
      if (!this.singleJudge(this.selections, '变更', true)) {
        return
      }
      this.activedData = this.selections[0]
      this.showAcceptorDlg = true
    },
    // 设置table setting
    setTableSetting(v) {
      switch (v) {
        case 'all':
          this.$set(this.tableConfig, 'setting', this.allSetting)
          break
        case 'waitApply':
          this.$set(this.tableConfig, 'setting', this.waitApplySetting)
          break
        default:
          this.$set(this.tableConfig, 'setting', this.otherSetting)
          break
      }
    },
    // 设置过滤条件
    setOtherFilters(v) {
      switch (v) {
        case 'all':
          this.typeFilters = [
            otherFiltersItem('acceptType', 'SOFT', '='),
            otherFiltersItem('isEnd', '0', '=')
          ]
          break
        case 'waitApply':
          this.typeFilters = [
            otherFiltersItem('acceptType', 'SOFT', '='),
            otherFiltersItem('isEnd', '0', '='),
            otherFiltersItem('acceptSteps', 'I1', '=')
          ]
          break
        case 'audit':
          this.typeFilters = [
            otherFiltersItem('acceptType', 'SOFT', '='),
            otherFiltersItem('isEnd', '0', '='),
            otherFiltersItem('acceptSteps', 'I2', '='),
            otherFiltersItem('isAccept', '1', '=')
          ]
          break
        case 'planCheck':
          this.typeFilters = [
            otherFiltersItem('acceptType', 'SOFT', '='),
            otherFiltersItem('isEnd', '0', '='),
            otherFiltersItem('acceptSteps', 'J3', '=')
          ]
          break
        case 'waitCheck':
          this.typeFilters = [
            otherFiltersItem('acceptType', 'SOFT', '='),
            otherFiltersItem('acceptSteps', '[J1]', 'in'),
            otherFiltersItem('isEnd', '0', '=')
          ]
          break
        case 'verify':
          this.typeFilters = [
            otherFiltersItem('acceptType', 'SOFT', '='),
            otherFiltersItem('isEnd', '0', '='),
            otherFiltersItem('acceptSteps', 'L2', '=')
          ]
          break
      }
    },
    refreshTableData(v = 'onPage') {
      this.$nextTick(() => {
        this.$refs.cTableBox.initTableData(v)
      })
    },
    selectionChange(v) {
      this.selections = v
    },
    // 删除
    clickRemoveHandle() {
      this.$confirm(`是否确认删除勾选的${this.selections.length}条数据？`, {
        type: 'warning',
        title: '删除'
      }).then(() => {
        console.log('删除成功！')
      })
    },
    // 跳转
    clickToRouterHandle(name) {
      if (!this.singleJudge(this.selections, '操作', true)) {
        return
      }
      if (name === 'CheckSoftApply') {
        queryDecorateTask(this.selections[0].decorationTaskCode).then(res => {
          if (res.data.records.length > 0 && res.data.records[0].schedule === 'D') {
            const s = this.selections[0]
            this.$router.push({ name, query: { id: s.id, acceptType: s.acceptType }})
          } else {
            this.$message.warning('软装陈列数据未完成，不可提交')
            return
          }
        })
      } else {
        const s = this.selections[0]
        this.$router.push({ name, query: { id: s.id, acceptType: s.acceptType }})
      }
    },
    // 详情
    toDetail(row) {
      this.$router.push({ name: 'CheckSoftDetail', query: { id: row.id, taskCode: row.decorationTaskCode, unitCode: row.constUnitCode }})
    }
  }
}
</script>
